package com.web.base.open.util;

import com.blue.jdbc.Page;
import com.web.base.open.message.RoleRequest;
import com.web.base.open.message.RoleResponse;
import com.web.base.open.model.Role;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 开放平台角色转换器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年02月27日
 */
public class RoleConvertor
{
	private RoleConvertor()
	{
	}
	
	public static Role toRole(RoleRequest request)
	{
		if (request == null)
			return null;
		
		Role role = new Role();
		role.setId(request.getId());
		role.setName(request.getName());
		role.setDescp(request.getDescp());
		role.setState(request.getState());
		return role;
	}
	
	public static RoleResponse toResponse(Role role)
	{
		if (role == null)
			return null;
		
		RoleResponse response = new RoleResponse();
		response.setId(role.getId());
		response.setName(role.getName());
		response.setDescp(role.getDescp());
		response.setState(role.getState());

		response.setSelect(role.getSelect());
		return response;
	}
	
	public static List<RoleResponse> toResponse(List<Role> list)
	{
		List<RoleResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (Role role : list)
		{
			responseList.add(toResponse(role));
		}
		return responseList;
	}
	
	public static Map<Integer, RoleResponse> toResponse(Map<Integer, Role> map)
	{
		Map<Integer, RoleResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;
		
		for (Map.Entry<Integer, Role> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue()));
		}
		return responseMap;
	}
	
	public static Page<RoleResponse> toResponse(Page<Role> page)
	{
		if (page == null)
			return null;
		
		Page<RoleResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList()));
		return responsePage;
	}
	
}
